package cn.edu.neusoft.model.impl;

import cn.edu.neusoft.config.PlatformConfig;

import cn.edu.neusoft.model.bean.CarPeccancy;
import cn.edu.neusoft.model.bean.User;
import cn.edu.neusoft.model.mapper.CarPeccancyMapper;
import cn.edu.neusoft.service.car.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CarPeccancyImpl {

    @Autowired
    private CarPeccancyHandler carPeccancyHandler;

    @Autowired
    private PlatformConfig platformConfig;
    @Autowired
    private CarPeccancyMapper carPeccancyMapper;

    private Map<Integer, String> map = new HashMap<>();
    private int maxSize = 0;

    public void getCarPeccancyByTime(long duration){

        long startTimestamp = -1;
        long endTimestamp = -1;

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime endDate = now;
        ZoneId zoneId = ZoneId.systemDefault(); // 使用系统默认时区
        ZonedDateTime endZonedDateTime = endDate.atZone(zoneId);
        java.time.Instant endInstant = endZonedDateTime.toInstant();
        endTimestamp = endInstant.getEpochSecond();

        // 获取当前时间
        if(duration == -1){
            int gap = Integer.parseInt(platformConfig.getPeccancyGap());
            // 计算startDate为当前时间往前推60秒
            LocalDateTime startDate = now.minusSeconds(gap);
            // 将LocalDateTime转换为ZonedDateTime（需要指定时区）
            ZonedDateTime startZonedDateTime = startDate.atZone(zoneId);
            // 将ZonedDateTime转换为Instant
            java.time.Instant startInstant = startZonedDateTime.toInstant();
            // 获取自1970年1月1日以来的秒数（时间戳）
            startTimestamp = startInstant.getEpochSecond();
        }
        else {
            startTimestamp = 1754035200;//2025.08.10 00:00:00
        }

        CarPeccancyRequest request = new CarPeccancyRequest();
        request.getData().setStartDate(startTimestamp);
        request.getData().setEndDate(endTimestamp);

        CarPeccancyResponse carPeccancyInfoByTime;

        int pageNum = 0;
        int total = 0;
        int nCount = 0;

        do
        {
            request.getData().setPage(++pageNum);
            carPeccancyInfoByTime = carPeccancyHandler.getCarPeccancyInfoByTime(request);
            total = carPeccancyInfoByTime.getData().getTotalSize();
            List<CarPeccancy> carPeccancyList = carPeccancyInfoByTime.getData().getList();
            for (int i = 0; i < carPeccancyList.size(); i++) {
                CarPeccancy carPeccancy = carPeccancyList.get(i);
                nCount++;
                if(map.containsValue(carPeccancy.getId())){
                    continue;
                }
                
                maxSize += 1;
                map.put(maxSize%1000,carPeccancy.getId());
                carPeccancyMapper.insertCarPeccancy(carPeccancy);
            }
            log.info("CarPeccancyImpl,getCarPeccancyByTime,pagenum:{},nCount:{},total:{}", pageNum,nCount,total);
        }while(nCount < total);
    }

    public void clear(){
        carPeccancyMapper.truncateCarPeccancy();
    }
}
